# Reference: <https://u-boot.readthedocs.io/en/latest/board/amlogic/radxa-zero.html>
# Maintainer: Ferass El Hafidi <funderscore@postmarketos.org>
pkgname=u-boot-bananapi-bpi-cm4
pkgver=2024.04
pkgrel=0
pkgdesc="U-Boot bootloader for BananaPi BPI-CM4"
url="https://source.denx.de/u-boot"
arch="aarch64"
license="GPL-2.0-or-later OFL-1.1 BSD-2-Clause BSD-3-Clause eCos-2.0 IBM-pibs
	ISC LGPL-2.0-only LGPL-2.1-only X11 proprietary"
makedepends="$depends_dev
	bc
	bison
	dtc
	flex
	openssl-dev
	py3-setuptools
	python3-dev
	swig
	bash
	qemu-x86_64
	"
options="!check"
# https://u-boot.readthedocs.io/en/latest/board/amlogic/pre-generated-fip.html
_amlogicblob_commit="8599bc77b17f38e69275f6145acc5792faab735e"

source="
	https://source.denx.de/u-boot/u-boot/-/archive/v$pkgver/u-boot-v$pkgver.tar.gz
	amlogicblobs-$_amlogicblob_commit.tar.gz::https://github.com/LibreELEC/amlogic-boot-fip/archive/$_amlogicblob_commit.tar.gz
"
builddir="$srcdir/u-boot-v$pkgver"
_fipbuilddir="$srcdir/amlogic-boot-fip-$_amlogicblob_commit"

build() {
	touch include/config.h
	LC_ALL=C date +'#define U_BOOT_DATE "%b %d %C%y"' > include/timestamp_autogenerated.h
	LC_ALL=C date +'#define U_BOOT_TIME "%T"' >> include/timestamp_autogenerated.h

	make bananapi-cm4-cm4io_defconfig
	make all
	cd $_fipbuilddir
	mkdir build
	./build-fip.sh bananapi-cm4io $builddir/u-boot.bin ./build
	cd ./build
	dd if=u-boot.bin.sd.bin of=u-boot.bin.sd-stripped.bin conv=fsync,notrunc bs=512 skip=1
}

package() {
	_installdir="$pkgdir/usr/share/u-boot/bananapi-bpi-cm4"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin.sd-stripped.bin" -t "$_installdir"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin.sd.bin" -t "$_installdir"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin" -t "$_installdir"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin.usb.bl2" -t "$_installdir"
	install -Dm644 "$_fipbuilddir/build/u-boot.bin.usb.tpl" -t "$_installdir"
}


sha512sums="
be9b0176337fe9321ac244a16a17dfc1fb976b019de36c78d52149fe7c9d89d2907321be4c0c2faf0d47a4982212501f29063dbd01a005ce2c54e072159e0e3e  u-boot-v2024.04.tar.gz
74c57daff859e7f449bd2743c7a8c0358b8dbe5d321ddb3a5b242b4d35683890d500d23e769490b1419a18bed233aea03ee11103bb4d646c502e4018283ba975  amlogicblobs-8599bc77b17f38e69275f6145acc5792faab735e.tar.gz
"
